package com.imu.chatmodule;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.imu.chatmodule.LongConnection.model.LongConnectionChatDTO;
import com.imu.chatmodule.LongConnection.model.LongConnectionLoginDTO;
import com.imu.chatmodule.LongConnection.util.LongConnectionMsgUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.net.InetSocketAddress;
import java.util.Scanner;

public class helloClient {
    public static void main(String[] args) throws InterruptedException, JsonProcessingException {
        //1.启动类
        ChannelFuture localhost = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override //链接建立后操作
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        nioSocketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4)).addLast(new StringDecoder()).addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                //System.out.println("getmsg"+msg);
                                super.channelRead(ctx, msg);
                            }
                        }); //字符串转为字节
                    }
                })
                .connect(new InetSocketAddress("localhost", 8889))
                .sync();
        ObjectMapper mapper = new ObjectMapper();
        Scanner scanner = new Scanner(System.in);
        while (true){

            String line = scanner.nextLine();
            if (line.equals("log")){
                LongConnectionLoginDTO loginDTO = new LongConnectionLoginDTO();
                //System.out.println("等待用户名");
                loginDTO.setSenderId(scanner.nextLine());
                byte[] bytes = mapper.writeValueAsBytes(loginDTO);
                ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
                buf.writeInt(bytes.length).writeBytes(bytes);
                localhost.channel().writeAndFlush(buf);

            }else{
                LongConnectionChatDTO chatDTO = new LongConnectionChatDTO();
                //System.out.println("等待消息");
                chatDTO.setMessage(scanner.nextLine());
                //System.out.println("等待接收方id");
                chatDTO.setReceiverId(scanner.nextLine());
                byte[] bytes = mapper.writeValueAsBytes(chatDTO);
                ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
                buf.writeInt(bytes.length).writeBytes(bytes);
                localhost.channel().writeAndFlush(buf);
            }






//            byte[] bytes = line.getBytes(StandardCharsets.UTF_8);


        }

    }
}